package com.hirain.javamail;

import org.junit.Test;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * Created by caicf on 2016/8/24.
 */
public class MailTest {

    private static final String MAIL_USER = "cai_cf";   //邮件服务器登录用户名

    private static final String MAIL_PASSWORD = "949502cai";   //邮件服务器登录密码

    private static final String MAIL_FROM_SMTP = "cai_cf@126.com";  //发送邮件地址

    public void sendmail(String[] mailArray,String subject,String content){
        Properties props = new Properties();
        //设置服务器验证
        props.setProperty("mail.smtp.auth", "true");
        //设置传输协议
        props.setProperty("mail.transport.protocol", "smtp");
        //选择服务类型
        props.setProperty("mail.host", "smtp.126.com");
        //通过认证创建一个session实例
        Session session = Session.getInstance(props,
                new Authenticator()
                {
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication(MAIL_USER,MAIL_PASSWORD);
                    }
                }
        );
        //显示邮件发送过程中的交互信息
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        Transport transport=null;
        try {
            //邮件发送人
            msg.setFrom(new InternetAddress(MAIL_FROM_SMTP));
            //邮件主题
            msg.setSubject(subject);
            //邮件内容
            msg.setText(content);
            int len=mailArray.length;
            InternetAddress address[]=new InternetAddress[len];
            for (int i = 0; i < mailArray.length; i++) {
                address[i]=new InternetAddress(mailArray[i]);
            }
            //邮件接收方
            msg.addRecipients(Message.RecipientType.TO, address);
            transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(transport!=null){
                    transport.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws Exception{
        MailTest demo2=new MailTest();
        String[] mailArray ={"chengfeng.cai@hirain.com","547461296@qq.com","yonggang.jian@hirain.com"
                ,"yang.Liu1@hirain.com","jiancai.wang@hirain.com","cai_cf@126.com"

        };
        String subject="Hirain邮箱发20160824";
        String content="Hello world啊  Hirain!";
        for (int i=0;i<1;i++){
            System.out.println("===============================>>>>>>>>>>>>>> "+i);
            Thread.sleep(1000);
            demo2.sendmail(mailArray,subject,content);
        }

    }


    @Test
    public void test() throws MessagingException {
            Properties props = new Properties();
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            Authenticator auth= new Authenticator(){
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName ="cai_cf";
                    String password ="949502cai";
                    return new PasswordAuthentication(userName, password);
                }
            };

            // 设置邮件服务器主机名
            props.setProperty("mail.host", "smtp.126.com");
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.smtp.port","25");  //端口
            // 设置环境信息
            Session session = Session.getInstance(props,auth);

            // 创建邮件对象

            Message msg = new MimeMessage(session);

            msg.setSubject(""+new Date()+" :RMU状态汇报");
            // 设置邮件内容
            //msg.setText("RMU ： "+rmuId+"\nstatus : "+status+"\ntimestamp : "+TimeStampUtil.timeStamp2Date(json.getLong("timestamp")));
            msg.setContent("RMU ： "+"\nstatus : "+"\ntimestamp : ","text/html;charset=gbk");
            // 设置发件人
            msg.setFrom(new InternetAddress("hirain_iov@163.com"));
        //设置收件人
            String[] address = new String[]{} ;
            msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress("yang.Liu1@hirain.com"));

//		InternetAddress[] copyAddress = new InternetAddress[address.length-1];
//		for (int i = 0; i < copyAddress.length; i++) {
//			copyAddress[i]=new InternetAddress(address[i+1]);
//		}
//		msg.setRecipients(MimeMessage.RecipientType.CC, copyAddress);
            // 连接邮件服务器
            Transport transport = session.getTransport();

            //用户名和密码   用户名指@之前的
            //transport.connect("ly2010001256", "2010001256");

            // 发送邮件
            transport.send(msg);
            // 关闭连接
            transport.close();
    }
}
